package com.llh.designpatterns.prototype;

/**
 * <h2>原型模式测试</h2>
 * <p>也是浅拷贝的测试</p>
 * <p>CreatedAt: 2019-11-27 21:42</p>
 * <p><a href="mailto:forever2y_y@163.com">SendMailToMe</a></p>
 *
 * @author llh
 */
class Client {
    public static void main(String[] args) throws CloneNotSupportedException {
        CloneSheep sheep = new CloneSheep();
        sheep.setAge(1);
        sheep.setName("Dolly");
        CloneSheep clone1 = (CloneSheep) sheep.clone();
        CloneSheep clone2 = (CloneSheep) sheep.clone();
        System.out.println(sheep + " " + sheep.getName());
        System.out.println(clone1 + " " + clone1.getAge());
        System.out.println(clone2 + " " + clone2.getName());
        shadeCopy();
    }

    /**
     * 浅拷贝测试
     * 成员变量为基本数据类型和字符串浅拷贝会进行值拷贝
     * 成员变量为引用类型，则只会拷贝引用
     */
    private static void shadeCopy() throws CloneNotSupportedException {
        CloneSheep sheep = new CloneSheep("Dolly", 2);
        sheep.setSon(new CloneSheep("Tom", 3));
        CloneSheep clone1 = (CloneSheep) sheep.clone();
        System.out.println(sheep.hashCode() + " son : " + sheep.getSon().hashCode());
        System.out.println(clone1.hashCode() + " son : " + clone1.getSon().hashCode());

    }
}
